﻿using System.Net.Mail;
using System.Text;
using VideoSeller.Core.Interfaces;

namespace VideoSeller.Business.Models
{
	public class MailModel
	{
		private readonly IMailProvider _mailProvider;

		public MailModel(IMailProvider mailProvider)
		{
			_mailProvider = mailProvider;
		}

		public bool SendRegistrationConfirmation(string login, string email, string confirmationUrl)
		{
			var subject = "Registration confirmation. Подтверждение регистрации.";
			var body = new StringBuilder("Dear {username}, to confirm registration please open url<br/><a href=\"{url}\">Confirm</a>");
            body.Append("<br />Уважаемый {username}, для подтверждения регистрации пройдите по ссылке:<br/><a href=\"{url}\">Подтвердить регистрацию</a>");
			body.Replace("{username}", login);
			body.Replace("{url}", confirmationUrl);

			var message = new MailMessage
			{
				To = { new MailAddress(email, login) },
				Subject = subject,
				Body = body.ToString(),
				SubjectEncoding = Encoding.UTF8
			};

			return _mailProvider.Send(message, true);
		}

		public bool SendResetPasswordConfirmation(string login, string email, string newPassword)
		{
			var subject = "Reset password";
			var body = new StringBuilder("Dear {username}, your password has been reset to {password}");

			body.Replace("{username}", login);
			body.Replace("{password}", newPassword);

			var message = new MailMessage
			{
				To = { new MailAddress(email, login) },
				Subject = subject,
				Body = body.ToString(),
				SubjectEncoding = Encoding.UTF8
			};

			return _mailProvider.Send(message, true);
		}
        public int SendInvitation(string text, string toList, string fromName)
        {
            int cnt = 0;
            bool ret = false;
            string[] aList = toList.Split('&');
            if (aList.Length > 0)
            {
                var subject = "VideoSeller Invitation from "+fromName;
                for (int i = 0; i < aList.Length; i++)
                {
                   ret = _mailProvider.Send( 
                    new MailMessage{
                        To = { new MailAddress(aList[i], "") },
                        Subject = subject,
                        Body = text,
                        SubjectEncoding = Encoding.UTF8
                    },
                    true);
                   cnt += ret ? 1 : -1;
                }
            }
            return cnt;
        }

		public bool SendFeedbackRequest(string login, string client_name, string email, string feedbackType, string content)
		{
			var subject = "Feedback - " + feedbackType;
			var message = new MailMessage
			{
				To = { new MailAddress("info@visell.tv") },
				From = new MailAddress(email, login),
				Subject = subject,
                Body = "Login:" + login +" Name: "+client_name+ " Email:" + email+"\n\nMessage:\n"+content,
				SubjectEncoding = Encoding.UTF8
			};

			return _mailProvider.Send(message, false);
		}
	}
}